package no.uia.ikt.gateway;
import java.awt.Component;
import java.util.Date;

import no.uia.ikt.p2p.ServiceClient;

import org.cybergarage.upnp.*;
import org.cybergarage.upnp.ssdp.*;
import org.cybergarage.upnp.device.*;
import org.cybergarage.upnp.event.*;

public class Gateway1 extends ControlPoint implements NotifyListener, EventListener, SearchResponseListener {
    
	private final static String INTEL_LIGHT_DEVICE_TYPE = "urn:schemas-upnp-org:device:BinaryLight:1";
	private final static String INTEL_LIGHT_SERVICE_TYPE = "urn:upnp-org:serviceId:SwitchPower.0001";
	
	private ServiceClient client = new ServiceClient();
	private Compositor compositor = new Compositor();

    public Gateway1() {
        addNotifyListener(this);
        addSearchResponseListener(this);
        addEventListener(this);
        search();
    }

    public Service getDeviceService(String deviceType, String serviceType) {
        Device dev = getDevice(deviceType);
        if (dev == null) {
            return null;
        }
        Service service = dev.getService(serviceType);
        if (service == null) {
            return null;
        }
        return service;
    }

    public boolean subscribeService(SSDPPacket packet, String deviceType, String serviceType) {
        Service service = getDeviceService(deviceType, serviceType);
        if (service == null) {
            return false;
        }
        return subscribe(service);
    }

    public void checkNewDevices(SSDPPacket packet) {
        subscribeService(packet, INTEL_LIGHT_DEVICE_TYPE, INTEL_LIGHT_SERVICE_TYPE);
        if (isDevice(packet, INTEL_LIGHT_SERVICE_TYPE) == true) {
            System.out.println("vv" + new Date()+" "+packet.getUSN()+" is a new device!");
        }
    }

    public void checkRemoveDevices(SSDPPacket packet) {
        if (isDevice(packet, INTEL_LIGHT_SERVICE_TYPE) == true) {
            System.out.println("**" + new Date()+packet.getUSN()+" is removed!");
        }
    }

    public boolean isDevice(SSDPPacket packet, String deviceType) {
        String usn = packet.getUSN();
        if (usn.endsWith(deviceType)) {
            return true;
        }
        return false;
    }

    public void printInfo(String info){
        System.out.println(info);
    }

    public void deviceNotifyReceived(SSDPPacket packet) {
        if (packet.isAlive() == true) {
            checkNewDevices(packet);
        }
        if (packet.isByeBye() == true) {
            checkRemoveDevices(packet);
        }
    }

    public void deviceSearchResponseReceived(SSDPPacket packet) {
    }
    private String airconTemp = "";
    private String message = "#";
    private Component comp;

    public void eventNotifyReceived(String uuid, long seq, String name, String value) {
        System.out.println("Notify UUID =" + uuid + ",Seq.No=" + seq + ",Name=" + name + ", Value=" + value);
        message = "Notify UUID =" + uuid + ",Seq.No=" + seq + ",Name=" + name + ", Value=" + value;
        Service service = this.getSubscriberService(uuid);
        if(service.getDevice()==null){
        	return;
        }
        Device device = service.getDevice();
        SSDPPacket sp = device.getSSDPPacket();
        String data = compositor.composite(sp, uuid, seq, name, value);
        client.startClient(data);
        System.out.println("sp is " + sp.toString());
        System.out.println("Send the packet");
    }

    public void powerOn(String deviceType) {
        Device dev = getDevice(deviceType);
        if (dev == null) {
            return;
        }
        Action getPowerAct = dev.getAction("GetPower");
        if (getPowerAct.postControlAction() == false) {
            return;
        }

        ArgumentList outArgList = getPowerAct.getOutputArgumentList();
        String powerState = outArgList.getArgument(0).getValue();
        String newPowerState = (powerState.compareTo("1") == 0) ? "0" : "1";

        Action setPowerAct = dev.getAction("SetPower");
        setPowerAct.setArgumentValue("Power", newPowerState);
        setPowerAct.postControlAction();
    }

    public Component getComp() {
        return comp;
    }

    public void setComp(Component comp) {
        this.comp = comp;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

